package com.ruanxie.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class ClassDetectEntity {
    private String studentId;
    private int bowCount;
    private int writeCount;
    private int listenCount;
    private int distractCount;
    private int readCount;
    private LocalDateTime detectBegin; // 检测开始时间
    private LocalDateTime detectEnd;   // 检测结束时间

    //TODO 注意：这里行为类别下标的映射关系
    //  优化：这里行为类别和行为名的映射，这里重复性代码有点多，应该是能够用放射来优化的
    /**
     * 使用学号+{行为类别下标:出现次数}初始化
     *
     * @param studentId
     * @param classCounts
     * @param detectBegin
     * @param detectEnd
     */
    public ClassDetectEntity(String studentId, Map<Integer, Integer> classCounts, LocalDateTime detectBegin, LocalDateTime detectEnd) {
        this.studentId = studentId;
        this.bowCount = classCounts.getOrDefault(0, 0);
        this.writeCount = classCounts.getOrDefault(1, 0);
        this.listenCount = classCounts.getOrDefault(2, 0);
        this.distractCount = classCounts.getOrDefault(3, 0);
        this.readCount = classCounts.getOrDefault(4, 0);
        this.detectBegin = detectBegin;
        this.detectEnd = detectEnd;
    }


//    public static Map<String,Map<Integer,Integer>> studentClassDetectCountMap;//key:studentId;value:{行为类别下标:出现次数}
//        //我需要将这个类型转换为一个List<ClassDetectEntity>类型，方便后续存进数据库中
    /**
     * studentClassDetectCountMap{studentId:{行为类别下标:出现次数}}
     * 转换成List<ClassDetectEntity>
     * @return
     */
    public static List<ClassDetectEntity> convertStudentClassDetectCountMapToEntities(Map<String,Map<Integer,Integer>> studentClassDetectCountMap,LocalDateTime detectBegin, LocalDateTime detectEnd) {
        List<ClassDetectEntity> entities = new ArrayList<>();
        for (Map.Entry<String, Map<Integer, Integer>> entry : studentClassDetectCountMap.entrySet()) {
            String studentId = entry.getKey();
            Map<Integer, Integer> classCounts = entry.getValue();
            entities.add(new ClassDetectEntity(studentId, classCounts, detectBegin, detectBegin));
        }
        return entities;
    }
}